package com.spzx.product.service.impl;

import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.service.ProductSpecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商品规格 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements ProductSpecService {

    @Autowired
    private CategoryBrandServiceImpl categoryBrandServiceImpl;

    @Override
    public List<ProductSpec> getPageList(ProductSpec productSpec) {
        return baseMapper.getPageList(productSpec);
    }

    @Override
    public ProductSpec getProductSpecById(Long id) {
        //获取商品规格
        ProductSpec productSpec = getById(id);
        //根据当前商品规格的分类id获取它的所有父id
        List<Long> parentIds = categoryBrandServiceImpl.getParentIds(productSpec.getCategoryId(), new ArrayList<>());
        //反转
        Collections.reverse(parentIds);
        //添加商品规格当前分类id
        parentIds.add(productSpec.getCategoryId());
        //将所有分类id设置到商品规格中
        productSpec.setCategoryIdList(parentIds);
        return productSpec;
    }
}
